<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var strStr = function (haystack, needle) {
            if (needle === "") return 0;
            let ans = 0, m = haystack.length, n = needle.length;
            for (let i = 0; i < m; i++) {
                if (haystack[i] === needle[ans]) {
                    if (ans === n - 1) return i - n + 1;
                    ans++;
                } else {
                    ans = 0;
                    if (haystack[i] === needle[ans]) ans++;
                }
            }
            return -1;
        };
        console.log(strStr("mississippi","issip"));
    </script>
</body>

</html>